// pages/distribution/sublevel.js
const app = getApp()
var mtabW;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 0,
        currentActive: 0,
        slideOffset: 0,
        tabs: [{
                name: '一级',
                amount: 0
            },
            {
                name: '二级',
                amount: 0
            },
        ],
        defaultPic: "https://img.zhxcxmall.com/ll/default-head-pic.png"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let mtabW = app.scrollWidth / 4; //设置tab的宽度
        this.setData({
            tabW: mtabW,
            slideOffset: mtabW / 2,
        })
        this.loadCount()
        this.loadData()
    },
    navClick(e) {
        let current = e.currentTarget.dataset.current
        let offsetW = e.currentTarget.offsetLeft;
        this.setData({
            current: current,
            slideOffset: offsetW + this.data.tabW / 2
        })
        this.loadData()
    },
    loadCount() {
        app.http.post('distributor/count-partner').then(res => {
            this.setData({
                [`tabs[0].amount`]: res.data.countFirst,
                [`tabs[1].amount`]: res.data.countSecond
            })
        })
    },
    loadData() {
        const {
            tabs = [], current = 0
        } = this.data
        let {
            page = 1, nextPage = true, list = [], loading = false,
        } = tabs[current]
        if (!nextPage || loading) {
            return
        }
        wx.showLoading({
            title: '加载中...',
        })
        this.setData({
            [`tabs[${current}].loading`]: true
        })
        let url = current == 0 ? 'distributor/first-partner' : current == 1 ? 'distributor/second-partner' : ''
        app.http.post(url + '?page=' + page).then(res => {
            page++
            nextPage = res.data.nextPage
            list.push(...res.data.list)
            loading = false
            this.setData({
                [`tabs[${current}]`]: Object.assign(tabs[current], {
                    page,
                    list,
                    loading,
                    nextPage,
                }),
            })
            wx.stopPullDownRefresh()
            wx.hideLoading()
        })
    },
    onReachBottom() {
        this.loadData()
    },
    onPullDownRefresh() {
        this.setData({
            [`tabs[${this.data.current}]`]: Object.assign(this.data.tabs[this.data.current], {
                page: 1,
                list: [],
                amount: 0,
                loading: false,
                nextPage: true,
            }),
        })
        this.loadCount()
        this.loadData()
    }
})